package com.dz.lm.web.controller.comp;

import com.alibaba.fastjson.JSONObject;
import com.dz.lm.common.dto.CompLabelDTO;
import com.dz.lm.common.exception.ServiceException;
import com.dz.lm.common.util.FileUploadUtil;
import com.dz.lm.common.util.TecentVideo;
import com.dz.lm.common.utils.StringUtils;
import com.dz.lm.common.utils.json.JsonUtils;
import com.dz.lm.domain.comp.Comp;
import com.dz.lm.domain.complabel.CompLabel;
import com.dz.lm.service.attach.AttachService;
import com.dz.lm.service.comp.label.CompLabelService;
import com.dz.lm.web.controller.Basic2Control;
import com.smartlbs.util.DateUtil;
import com.smartlbs.util.StringUtil;
import com.smartlbs.util.UploadFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.8;
 * @author: LDY;
 * @Description;
 * @Date: Created in 2020/6/6 14:09;
 */
@Controller
@RequestMapping("/rewriteUrl/complabel")
public class CompLabelController extends Basic2Control {
    @Autowired
    private CompLabelService compLabelService;
    @Autowired
    private AttachService attachService;
    /**
     * 添加公司标签
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/add")
    public Map add(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        Map<String, Object> param = JsonUtils.readValue(reqData.toString());
        Object imgUrl = request.getAttribute("imgUrl");
        //附件
        List<UploadFile> fList = (List<UploadFile>) request.getAttribute("fileField");

        Map<String, Object> urlMap = new HashMap<String, Object>();
        if (fList != null && fList.size() > 0) {
            urlMap = FileUploadUtil.uploadByInStream(fList, imgUrl, "label");
        }
        //公司id
        int compId = param.get("compId") != null ? StringUtil.getInt(param.get("compId").toString(), 0) : 0;
        //主题
        String topic = param.get("topic") != null ? param.get("topic").toString() : "";

        //标题
        String title = param.get("title") != null ? param.get("title").toString() : "";
        //内容
        String content = param.get("content") != null ? param.get("content").toString() : "";
        //视频标题
        String videoTitle = param.get("videoTitle") != null ? param.get("videoTitle").toString() : "";
        //图片标图
        String photoTitle = param.get("photoTitle") != null ? param.get("photoTitle").toString() : "";
        //视频id
        int video = param.get("video") != null ? StringUtil.getInt(param.get("video").toString(), 0) : 0;
        if (compId == 0 || StringUtils.isEmpty(topic)) {
            message = "参数有误";
        } else {
            CompLabel label = new CompLabel();
            label.setCompId(compId);
            label.setTitle(title);
            label.setContent(content);
            label.setVideo(video);
            label.setTopic(topic);
            label.setPhotoTitle(photoTitle);
            label.setVideoTitle(videoTitle);
            label.setCreateTime(com.dz.lm.common.utils.DateUtil.getTimeStr(new Date()));
            label.setUrl1(urlMap.get("url1") != null ? imgUrl + urlMap.get("url1").toString() : "");
            label.setUrl2(urlMap.get("url2") != null ? imgUrl + urlMap.get("url2").toString() : "");
            label.setUrl3(urlMap.get("url3") != null ? imgUrl + urlMap.get("url3").toString() : "");
            label.setUrl4(urlMap.get("url4") != null ? imgUrl + urlMap.get("url4").toString() : "");
            label.setUrl5(urlMap.get("url5") != null ? imgUrl + urlMap.get("url5").toString() : "");
            label.setUrl6(urlMap.get("url6") != null ? imgUrl + urlMap.get("url6").toString() : "");
            label.setUrl7(urlMap.get("url7") != null ? imgUrl + urlMap.get("url7").toString() : "");
            label.setUrl8(urlMap.get("url8") != null ? imgUrl + urlMap.get("url8").toString() : "");
            label.setUrl9(urlMap.get("url9") != null ? imgUrl + urlMap.get("url9").toString() : "");
            label.setUrl9(urlMap.get("file") != null ? imgUrl + urlMap.get("file").toString() : "");
            int add = compLabelService.add(label);
            if (add > 0) {
                result = "00";
                message = "操作成功";
            }

        }
        subjo.put("message", message);
        subjo.put("result", result);
        subjo.put("data", data);
        return subjo;
    }

    /**
     * 详情
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/get")
    public Map get(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        Map<String, Object> param = JsonUtils.readValue(reqData.toString());
        Object imgUrl = request.getAttribute("imgUrl");
        Integer labelId = param.get("labelId") != null ? Integer.parseInt(param.get("labelId").toString()) : 0;
        if (labelId > 0) {
            data = compLabelService.get(labelId);
            result = "00";
            message = "获取成功";
        } else {
            message = "参数有误";
        }

        subjo.put("message", message);
        subjo.put("result", result);
        subjo.put("data", data);
        return subjo;
    }

    /**
     * 修改
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/update")
    public Map updateCode(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        Map<String, Object> param = JsonUtils.readValue(reqData.toString());
        Object imgUrl = request.getAttribute("imgUrl");
        //附件
        List<UploadFile> fList = (List<UploadFile>) request.getAttribute("fileField");

        Map<String, Object> urlMap = new HashMap<String, Object>();
        if (fList != null && fList.size() > 0) {
            urlMap = FileUploadUtil.uploadByInStream(fList, imgUrl, "label");
        }

        Integer labelId = param.get("labelId") != null ? Integer.parseInt(param.get("labelId").toString()) : 0;
        //主题
        String topic = param.get("topic") != null ? param.get("topic").toString() : "";
        //标题
        String title = param.get("title") != null ? param.get("title").toString() : "";
        //内容
        String content = param.get("content") != null ? param.get("content").toString() : "";
        //视频标题
        String videoTitle = param.get("videoTitle") != null ? param.get("videoTitle").toString() : "";
        //图片标图
        String photoTitle = param.get("photoTitle") != null ? param.get("photoTitle").toString() : "";
        //视频
        int video = param.get("video") != null ? StringUtil.getInt(param.get("video").toString(), 0) : 0;
        //删除附件地址,逗号分隔
        String deFiles = param.get("deFiles") != null ? param.get("deFiles").toString() : "";
        //删除视频id
        String deFileId = param.get("deFileId") != null ? param.get("deFileId").toString() : "";
        String url1 = param.get("url1") != null ? param.get("url1").toString() : "";
        String url2 = param.get("url2") != null ? param.get("url2").toString() : "";
        String url3 = param.get("url3") != null ? param.get("url3").toString() : "";
        String url4 = param.get("url4") != null ? param.get("url4").toString() : "";
        String url5 = param.get("url5") != null ? param.get("url5").toString() : "";
        String url6 = param.get("url6") != null ? param.get("url6").toString() : "";
        String url7 = param.get("url7") != null ? param.get("url7").toString() : "";
        String url8 = param.get("url8") != null ? param.get("url8").toString() : "";
        String url9 = param.get("url9") != null ? param.get("url9").toString() : "";
        String file = param.get("file") != null ? param.get("file").toString() : "";

        //删除附件表id
        Integer deVideoId = param.get("deVideoId") != null ? Integer.parseInt(param.get("deVideoId").toString()) : 0;
        if ( labelId == 0) {
            message = "参数有误";
        } else {
            CompLabelDTO dto = compLabelService.get(labelId);
            CompLabel label = new CompLabel();
            label.setLabelId(labelId);
            label.setTopic(topic);
            label.setPhotoTitle(photoTitle);
            label.setVideoTitle(videoTitle);
            label.setTitle(title);
            label.setContent(content);
            label.setVideo(video);
            label.setUrl1(url1);
            label.setUrl2(url2);
            label.setUrl3(url3);
            label.setUrl4(url4);
            label.setUrl5(url5);
            label.setUrl6(url6);
            label.setUrl7(url7);
            label.setUrl8(url8);
            label.setUrl9(url9);
            label.setFile(file);
            try {
                if(urlMap.get("url1") != null){
                    label.setUrl1(urlMap.get("url1") != null ? imgUrl + urlMap.get("url1").toString() : "");
                }
                if(urlMap.get("url2") != null ){
                    label.setUrl2(urlMap.get("url2") != null ? imgUrl + urlMap.get("url2").toString() : "");

                }
                if(urlMap.get("url3") != null ){
                    label.setUrl3(urlMap.get("url3") != null ? imgUrl + urlMap.get("url3").toString() : "");

                }
                if(urlMap.get("url4") != null){
                    label.setUrl4(urlMap.get("url4") != null ? imgUrl + urlMap.get("url4").toString() : "");

                }
                if(urlMap.get("url5") != null){
                    label.setUrl5(urlMap.get("url5") != null ? imgUrl + urlMap.get("url5").toString() : "");

                }
                if(urlMap.get("url6") != null ){
                    label.setUrl6(urlMap.get("url6") != null ? imgUrl + urlMap.get("url6").toString() : "");

                }
                if(urlMap.get("url7") != null ){
                    label.setUrl7(urlMap.get("url7") != null ? imgUrl + urlMap.get("url7").toString() : "");

                }
                if(urlMap.get("url8") != null ){
                    label.setUrl8(urlMap.get("url8") != null ? imgUrl + urlMap.get("url8").toString() : "");

                }
                if(urlMap.get("url9") != null ){
                    label.setUrl9(urlMap.get("url9") != null ? imgUrl + urlMap.get("url9").toString() : "");

                }
                if(urlMap.get("file") != null ){
                    label.setFile(urlMap.get("file") != null ? imgUrl + urlMap.get("file").toString() : "");

                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            label.setUpdateTime(com.dz.lm.common.utils.DateUtil.getTimeStr(new Date()));
            int i = compLabelService.update(label);
            if (i > 0) {
                if (!StringUtils.isEmpty(deFileId)) { //删除视频
                    TecentVideo.delete(deFileId);
                    attachService.delete(deVideoId);
                }
                if(!com.smartlbs.util.StringUtil.containEmpty(deFiles)){
                    try {
                        for (String s : deFiles.split(",")) {
                            FileUploadUtil.deleteFile(s);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                result = "00";
                message = "修改成功";
            }
        }

        subjo.put("message", message);
        subjo.put("result", result);
        subjo.put("data", data);
        return subjo;
    }
    /**
     * 详情
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/list")
    public Map list(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        Map<String, Object> param = JsonUtils.readValue(reqData.toString());
        Object imgUrl = request.getAttribute("imgUrl");
        //公司
        Integer compId = param.get("compId") != null ? Integer.parseInt(param.get("compId").toString()) : 0;
        Integer pageNum = param.get("pageNum") != null ? Integer.parseInt(param.get("pageNum").toString()) : 1;
        Integer rows = param.get("rows") != null ? Integer.parseInt(param.get("rows").toString()) : 10;
        if (compId > 0) {
            data = compLabelService.query(pageNum,rows,compId);
            result = "00";
            message = "获取成功";
        } else {
            message = "参数有误";
        }

        subjo.put("message", message);
        subjo.put("result", result);
        subjo.put("data", data);
        return subjo;
    }
    /**
     * 详情
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/delete")
    public Map delete(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        Map<String, Object> param = JsonUtils.readValue(reqData.toString());
        Object imgUrl = request.getAttribute("imgUrl");
        Integer labelId = param.get("labelId") != null ? Integer.parseInt(param.get("labelId").toString()) : 0;
        if (labelId > 0) {
            int delete = compLabelService.delete(labelId);
            if(delete > 0){
                result = "00";
                message = "操作成功";
            }

        } else {
            message = "参数有误";
        }

        subjo.put("message", message);
        subjo.put("result", result);
        subjo.put("data", data);
        return subjo;
    }
}
